﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using HiTeachers.Entities;
using HiTeachers.Services;

namespace HiTeachers.Web
{
    public partial class AdminStdFeedback : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;
        }

        protected void BtnShowUnShowClick(object sender, EventArgs e)
        {
            if (Request.Form["chkItem"] == null) return;

            string selectedItems = Request.Form["chkItem"];
            string[] selectedItemIds = selectedItems.Split(',');
            foreach (Entities.StudentFeedback studentFeedback in selectedItemIds.Select(t => new Guid(t)).Select(itemId => StudentFeedbackServices.GetById(itemId)))
            {
                studentFeedback.IsReviewed = !studentFeedback.IsReviewed;

                StudentFeedbackServices.SaveOrUpdate(studentFeedback);
            }

            gridData.DataBind();
            udpGrid.DataBind();
        }

        protected void BtnDeleteClick(object sender, EventArgs e)
        {
            if (Request.Form["chkItem"] == null) return;

            string selectedItems = Request.Form["chkItem"];
            string[] selectedItemIds = selectedItems.Split(',');
            foreach (var itId in selectedItemIds.Select(itemId => new Guid(itemId))) StudentFeedbackServices.Delete(itId);

            gridData.DataBind();
            udpGrid.DataBind();
        }

        protected void GridDataItemCommand(object sender, CommandEventArgs e)
        {
            string commandName = e.CommandName;
            string commandArgument = e.CommandArgument.ToString();

            if (commandArgument == string.Empty) return;

            var guidId = new Guid(commandArgument);
            Entities.StudentFeedback studentFeedback = StudentFeedbackServices.GetById(guidId);

            switch (commandName)
            {
                case "Delete":
                    //StudentFeedbackServices.Delete(studentFeedback);
                    StudentFeedbackServices.Delete(guidId);

                    break;
                case "Show":
                    studentFeedback.IsReviewed = true;
                    StudentFeedbackServices.SaveOrUpdate(studentFeedback);

                    break;
                case "UnShow":
                    studentFeedback.IsReviewed = false;
                    StudentFeedbackServices.SaveOrUpdate(studentFeedback);

                    break;
                //default:
                //    Response.Redirect("Default.aspx");
                //    break;
            }

            //Update Rating for teacher
            Teacher teacher = studentFeedback.Teacher;
            teacher.Rate = StudentFeedbackServices.GetAvgRatingOnTeacher(teacher.ID);
            TeacherServices.SaveOrUpdate(teacher);

            udpGrid.DataBind();
        }
    }
}
